package com.clive.ttweb.service.impl;

import com.clive.ttweb.bean.Dishes;
import com.clive.ttweb.excption.DishException;
import com.clive.ttweb.mapper.DishesMapper;
import com.clive.ttweb.service.DishesService;
import com.clive.ttweb.vo.DishesVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class DishesServiceImpl implements DishesService {

    @Autowired
    private DishesMapper dishesMapper;

    @Override
    public int addDishes(Dishes dishes) {
        if (dishes.getCategory().getId() == null) {
            throw new DishException("菜品分类不能为空");
        }
        dishes.setBuyCount(1);
        dishes.setStatus(1);
        Date date = new Date();
        dishes.setCreated(date);
        dishes.setUpdated(date);
        int i = dishesMapper.addDishes(dishes);
        if (i <= 0) {
            throw new DishException("添加菜品失败");
        }

        return i;
    }

    @Override
    public DishesVo queryDish(Integer currentPage, Integer limit,Dishes dishes) {
        DishesVo vo = new DishesVo();
        List<Dishes> list = dishesMapper.findCurrentPageDish((currentPage-1)*limit,limit,dishes);
        vo.setDishess(list);
        int dishesTotal = dishesMapper.findDishesTotal(dishes);
        vo.setDishesTotal(dishesTotal);
        return vo;
    }

    @Override
    public int deleteDishById(Integer id) {
        int i = dishesMapper.deleteDishById(id);
        if(i<=0){
            throw new DishException("删除菜品失败");
        }
        return i;
    }

    @Override
    public Dishes findDishOne(Integer id) {
        Dishes dishes = dishesMapper.findDishOne(id);
        return dishes;
    }

    @Override
    public int updateDish(Dishes dishes) {
        if(dishes.getCategory().getId() == 0){
            throw new DishException("菜品分类不能为空");
        }
        Date date = new Date();

        dishes.setUpdated(date);
        int i = dishesMapper.updateDish(dishes);
        if(i<=0){
            throw new DishException("修改菜品失败");
        }
        return i;
    }
}